package com.hangzhou.xyj.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.manager.IntentManager;
import com.hangzhou.xyj.model.UserModel;
import com.hangzhou.xyj.model.type.EIntentTagType;
import com.hangzhou.xyj.ui.activity.SingingListActivity;
import com.hangzhou.xyj.ui.adapter.base.AbsBaseAdapter;
import com.hangzhou.xyj.ui.widget.CircleImageView;
import com.hangzhou.xyj.ui.widget.views.MyTextView;
import com.hangzhou.xyj.utils.constants.DataKey;

import java.util.List;

/**
 * Created by DT-038 on 2016/1/6.
 * 更多名家adapter
 */
public class ExpertListAdapter extends AbsBaseAdapter<UserModel> {

    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private int mIntentTag;

    public ExpertListAdapter(Context context, List<UserModel> data) {
        super(context, data);
        mLayoutInflater = LayoutInflater.from(context);
        mContext = context;
    }

    public void setIntentTag(int intentTag) {
        this.mIntentTag = intentTag;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        RecyclerView.ViewHolder viewHolder;
        switch (viewType) {
            default:
                viewHolder = new MoreExpertViewHolder(mLayoutInflater.inflate(R.layout.layout_more_expert_item, viewGroup, false));
                break;
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        if (viewHolder == null) {
            return;
        }
        if (viewHolder instanceof MoreExpertViewHolder) {
            ((MoreExpertViewHolder) viewHolder).setData(mDataList.get(position));
        }
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    class MoreExpertViewHolder extends RecyclerView.ViewHolder {
        private MyTextView mTvExpert;
        private CircleImageView mCivHeadImg;
        private View mTopPadding;
        private View mBotttomPadding;
        private View mUnderLine;

        public MoreExpertViewHolder(View itemView) {
            super(itemView);
            initView(itemView);
        }

        private void initView(View itemView) {
            mTvExpert = (MyTextView) itemView.findViewById(R.id.tv_more_expert_name);
            mCivHeadImg = (CircleImageView) itemView.findViewById(R.id.iv_more_expert_head);
            mTopPadding = itemView.findViewById(R.id.v_top_padding);
            mBotttomPadding = itemView.findViewById(R.id.v_bottom_padding);
            mUnderLine = itemView.findViewById(R.id.v_underline);
        }

        public void setData(final UserModel data) {
            if (data.isLastOne() && data.getIsAreaFirst()) {
                mBotttomPadding.setVisibility(View.VISIBLE);
                mUnderLine.setVisibility(View.GONE);
                mTopPadding.setVisibility(View.VISIBLE);
            } else if (data.isLastOne()) {
                mBotttomPadding.setVisibility(View.VISIBLE);
                mUnderLine.setVisibility(View.GONE);
            } else if (data.getIsAreaFirst() && data.isAreaLast()) {
                mTopPadding.setVisibility(View.VISIBLE);
                mUnderLine.setVisibility(View.GONE);
            } else if (data.getIsAreaFirst()) {
                mTopPadding.setVisibility(View.VISIBLE);
            } else if (data.isAreaLast()) {
                mUnderLine.setVisibility(View.GONE);
            } else {
            }
            mTvExpert.setText(data.getName());
            mCivHeadImg.displayImage(data.getHeadPic());

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent;
                    switch (EIntentTagType.valueOf(mIntentTag)) { //判断需要跳转到哪里去
                        case TAG_DIAN_DRAMA_EXPERT://点戏进来
                            intent = new Intent(mContext, SingingListActivity.class);
                            intent.putExtra(DataKey.TITLE, data.getName());
                            intent.putExtra(DataKey.INDEX, data.getUserId());
                            intent.putExtra(DataKey.INTENT_TAG, EIntentTagType.TAG_DIAN_DRAMA_EXPERT.getValue());
                            IntentManager.getInstance().goActivity(mContext, intent);
                            break;
                        case TAG_STAGE_EXPERT://戏台-名家 进入
                        case TAG_DRAMA_GROUP://发现-剧团 进入
                        case TAG_STAGE_TEAM://戏台-名团 进入
                        default:
                            IntentManager.getInstance().goExpertInfoActivity(mContext, Integer.valueOf(data.getUserId()),
                                    EIntentTagType.valueOf(mIntentTag));
                    }
                }
            });
        }
    }
}
